import 'package:flutter/material.dart';
import '../models/stock_item.dart';

class StockEntryForm extends StatefulWidget {
  final Function(StockItem) onItemAdded;

  const StockEntryForm({
    Key? key,
    required this.onItemAdded,
  }) : super(key: key);

  @override
  State<StockEntryForm> createState() => _StockEntryFormState();
}

class _StockEntryFormState extends State<StockEntryForm> {
  final _formKey = GlobalKey<FormState>();
  final _nameController = TextEditingController();
  final _categoryController = TextEditingController();
  final _unitController = TextEditingController();
  final _priceController = TextEditingController();
  final _quantityController = TextEditingController();
  final _supplierController = TextEditingController();
  final _notesController = TextEditingController();

  @override
  void dispose() {
    _nameController.dispose();
    _categoryController.dispose();
    _unitController.dispose();
    _priceController.dispose();
    _quantityController.dispose();
    _supplierController.dispose();
    _notesController.dispose();
    super.dispose();
  }

  void _submitForm() {
    if (_formKey.currentState!.validate()) {
      final item = StockItem(
        id: DateTime.now().millisecondsSinceEpoch.toString(),
        name: _nameController.text,
        category: _categoryController.text,
        unit: _unitController.text,
        price: double.parse(_priceController.text),
        quantity: int.parse(_quantityController.text),
        supplier: _supplierController.text,
        entryDate: DateTime.now(),
        notes: _notesController.text,
      );

      widget.onItemAdded(item);
      _formKey.currentState!.reset();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Form(
      key: _formKey,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          TextFormField(
            controller: _nameController,
            decoration: const InputDecoration(
              labelText: 'مەھسۇلات ئىسمى',
              icon: Icon(Icons.shopping_bag),
            ),
            validator: (value) {
              if (value == null || value.isEmpty) {
                return 'مەھسۇلات ئىسمىنى كىرگۈزۈڭ';
              }
              return null;
            },
          ),
          const SizedBox(height: 16),
          TextFormField(
            controller: _categoryController,
            decoration: const InputDecoration(
              labelText: 'تۈرى',
              icon: Icon(Icons.category),
            ),
            validator: (value) {
              if (value == null || value.isEmpty) {
                return 'تۈرىنى كىرگۈزۈڭ';
              }
              return null;
            },
          ),
          const SizedBox(height: 16),
          TextFormField(
            controller: _unitController,
            decoration: const InputDecoration(
              labelText: 'ئۆلچەم بىرلىكى',
              icon: Icon(Icons.straighten),
            ),
            validator: (value) {
              if (value == null || value.isEmpty) {
                return 'ئۆلچەم بىرلىكىنى كىرگۈزۈڭ';
              }
              return null;
            },
          ),
          const SizedBox(height: 16),
          TextFormField(
            controller: _priceController,
            decoration: const InputDecoration(
              labelText: 'باھاسى',
              icon: Icon(Icons.attach_money),
            ),
            keyboardType: TextInputType.number,
            validator: (value) {
              if (value == null || value.isEmpty) {
                return 'باھاسىنى كىرگۈزۈڭ';
              }
              if (double.tryParse(value) == null) {
                return 'توغرا سان كىرگۈزۈڭ';
              }
              return null;
            },
          ),
          const SizedBox(height: 16),
          TextFormField(
            controller: _quantityController,
            decoration: const InputDecoration(
              labelText: 'سانى',
              icon: Icon(Icons.format_list_numbered),
            ),
            keyboardType: TextInputType.number,
            validator: (value) {
              if (value == null || value.isEmpty) {
                return 'سانىنى كىرگۈزۈڭ';
              }
              if (int.tryParse(value) == null) {
                return 'توغرا سان كىرگۈزۈڭ';
              }
              return null;
            },
          ),
          const SizedBox(height: 16),
          TextFormField(
            controller: _supplierController,
            decoration: const InputDecoration(
              labelText: 'تەمىنلىگۈچى',
              icon: Icon(Icons.business),
            ),
            validator: (value) {
              if (value == null || value.isEmpty) {
                return 'تەمىنلىگۈچىنى كىرگۈزۈڭ';
              }
              return null;
            },
          ),
          const SizedBox(height: 16),
          TextFormField(
            controller: _notesController,
            decoration: const InputDecoration(
              labelText: 'ئىزاھات',
              icon: Icon(Icons.note),
            ),
            maxLines: 3,
          ),
          const SizedBox(height: 24),
          SizedBox(
            width: double.infinity,
            child: ElevatedButton(
              onPressed: _submitForm,
              style: ElevatedButton.styleFrom(
                backgroundColor: Colors.orange,
                padding: const EdgeInsets.symmetric(vertical: 16),
              ),
              child: const Text(
                'قوشۇش',
                style: TextStyle(fontSize: 16),
              ),
            ),
          ),
        ],
      ),
    );
  }
} 